//
//  XKLPublishViewController.m
//  GuiZiShouDuanZi
//
//  Created by xmg on 16/4/24.
//  Copyright © 2016年 xiao_kl. All rights reserved.
//

#import "XKLPublishView.h"
#import "XKLLonginBut.h"
#import <POP.h>

@implementation XKLPublishView

static UIWindow *window_;

+(void)show{
    
    //创建窗口
    window_=[[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    window_.hidden=NO;
    
    XKLPublishView *publishV=[[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] firstObject];
    //设置view的大小
    publishV.frame=window_.bounds;
    //添加到窗口
    [window_ addSubview:publishV];
    
}

//动画间隔时间
CGFloat timerSpace=0.1;
-(void)awakeFromNib{
    
    //开始不可以点击
    self.userInteractionEnabled=NO;
    
    //创建的时候添加按钮和图片
    
    NSArray *images=@[@"publish-video",@"publish-picture",@"publish-text",@"publish-audio",@"publish-review",@"publish-offline"];
    NSArray *titles=@[@"发视频",@"发图片",@"发段子",@"发声音",@"审帖",@"离线下载"];
    
    //按钮宽高
    CGFloat butW=72;
    CGFloat butH=butW+30;
    //最大列数
    int maxC=3;
    CGFloat beginX=15;
    CGFloat space=(ScreenW-beginX*2-maxC*butW)/(maxC-1);
    CGFloat beginY=([UIScreen mainScreen].bounds.size.height-butH*2)*0.5;
    
    
    for (int i=0; i<images.count; i++) {
        XKLLonginBut *but=[[XKLLonginBut alloc] init];
        //按钮设置
        [but addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];
        [but setImage:[UIImage imageNamed:images[i]] forState:UIControlStateNormal];
        but.titleLabel.font=[UIFont systemFontOfSize:15];
        [but setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [but setTitle:titles[i] forState:UIControlStateNormal];
        [self addSubview:but];
        but.tag=i;
        
        //设置位置
        CGFloat butX=beginX + (space + butW) * (i % maxC);
        CGFloat butY=beginY +  butH * (i/maxC);
        
        
        //开启动画
        POPSpringAnimation *animation=[POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
        
        //弹性系数
        animation.springBounciness=5;
        animation.springSpeed=5;
        animation.fromValue=[NSValue valueWithCGRect:CGRectMake(butX, butY-ScreenH, butW, butH)];
        animation.toValue=[NSValue valueWithCGRect:CGRectMake(butX, butY, butW, butH)];
        animation.beginTime=CACurrentMediaTime()+i*timerSpace;
        
        [but pop_addAnimation:animation forKey:nil];
        
    }
    
    
    UIImageView *imageV=[[UIImageView alloc] init];
    imageV.image=[UIImage imageNamed:@"app_slogan"];
    imageV.contentMode=UIViewContentModeScaleAspectFit;
    [self addSubview:imageV];
    CGFloat centerY=ScreenH * 0.2;
    //动画
    POPSpringAnimation *spring=[POPSpringAnimation animationWithPropertyNamed:kPOPViewFrame];
    spring.springBounciness=5;
    spring.springSpeed=5;
    spring.fromValue=[NSValue valueWithCGRect:CGRectMake(0, centerY-ScreenH, ScreenW, 30)];
    spring.toValue=[NSValue valueWithCGRect:CGRectMake(0, centerY, ScreenW, 30)];
    spring.beginTime=CACurrentMediaTime() + images.count*timerSpace;
    [spring setCompletionBlock:^(POPAnimation *animation, BOOL is) {
        self.userInteractionEnabled=YES;
    }];
    [imageV pop_addAnimation:spring forKey:nil];
}

-(void)butClick:(UIButton *)but{
    [self cancleWindow:^{
        if (but.tag==0) {
            NSLog(@"发视频");
        }else if (but.tag==1){
            NSLog(@"打电话");
        }
    }];
}
- (IBAction)cancle:(id)sender {
    [self cancleWindow:nil];
    
}

-(void)cancleWindow:(void(^)())block{
    //取消之后禁止操作
    self.userInteractionEnabled=NO;
    
    //遍历执行动画
    //开始值
    int beginIndex=2;
    for (int i=beginIndex; i<self.subviews.count; i++) {
        UIView *view=self.subviews[i];
        //动画
        POPBasicAnimation *basic=[POPBasicAnimation animationWithPropertyNamed:kPOPViewFrame];
        basic.fromValue=[NSValue valueWithCGRect:view.frame];
        
        basic.toValue=[NSValue valueWithCGRect:CGRectMake(view.x, view.y+ScreenH, view.width, view.height)];
       
        basic.beginTime=CACurrentMediaTime()+(i-2)*timerSpace;
        //判断是否是最后一个
        if(i==(self.subviews.count-1)){
            [basic setCompletionBlock:^(POPAnimation *animation, BOOL is) {
                
                if (block!=nil) block();
                
                window_.hidden=YES;
                window_=nil;
            }];
        }
        
        [view pop_addAnimation:basic forKey:nil];
        
    }
    
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self cancleWindow:nil];
}
@end
